package com.meirimoney.app;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.meirimoney.app.control.MenuBar;
import com.meirimoney.sdk.models.Commodity;
import com.meirimoney.task.CommodityTask;

/***
 * 兑换中心
 * 
 * @author gewei
 * 
 */
public class CommodityList extends BaseActivity implements OnItemClickListener
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.initializeUI();
		this.setTitle("兑换中心");
		this.load();
	}

	/***
	 * 初始化当前界面的UI
	 */
	private void initializeUI()
	{
		this.setContentView(R.layout.mrm_commodity);
		if (null == this.navigation)
		{
			this.navigation = (MenuBar) this.findViewById(R.id.head);
		}
		if (null == this.list)
		{
			this.list = (ListView) this.findViewById(R.id.commodityList);
			this.list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
			this.list.setOnItemClickListener(this);
		}
	}

	private void load()
	{
		if (this.task != null)
		{
			AsyncTask.Status status = this.task.getStatus();
			if (status != AsyncTask.Status.FINISHED)
			{
				return;
			}
		}
		this.task = new CommodityTask(this, this.list);
		this.task.execute(0);
	}

	private ListView list;

	private CommodityTask task;

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3)
	{
		Adapter adapter = this.list.getAdapter();
		Commodity commodity = (Commodity) adapter.getItem(position);
		Intent i = null;
		if (commodity.Category == 1)
		{
			i = new Intent(this, PhoneRecharge.class);
		} else if (commodity.Category == 2)
		{
			i = new Intent(this, AlipayWithdraw.class);
		}
		i.putExtra("commodity", this.getBundle(commodity));
		this.startActivity(i);
	}

	/***
	 * 根据商品对象创建包裹对象
	 * 
	 * @param commodity
	 *            商品对象
	 * @return 包裹对象
	 */
	public Bundle getBundle(Commodity commodity)
	{
		Bundle bd = new Bundle();
		bd.putInt("category", commodity.Category);
		bd.putInt("integral", commodity.Integral);
		bd.putDouble("price", commodity.Price);
		bd.putLong("id", commodity.Id);
		return bd;
	}
}
